using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace VisualPureBasicIDE.CoreGUI
{
    [TypeConverter(typeof(ExpandableObjectConverter))]
    [ToolboxItem(true)]
    public partial class OptionsPanel : UserControl
    {
        private bool _optionsUpdated;

        [Browsable(true)]
        [Description("This event is fired when the panel options are changed")]
        public event EventHandler OptionsChanged;

        #region Properties

        /// <summary>
        /// Gets the options form.
        /// </summary>
        /// <value>The options form.</value>
        [Browsable(false)]
        public OptionsForm OptionsForm { get; private set; }

        [Category("Options Form")]
        [Description(
            "The path of the OptionsPanel (determines the location in the Category TreeView in the parent OptionsForm form)"
            )]
        public String CategoryPath { get; set; }

        [Category("Options Form")]
        [Description("The name displayed for this panel in the Category TreeView in the parent OptionsForm form")]
        public String DisplayName { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether [options changed].
        /// </summary>
        /// <value><c>true</c> if [options changed]; otherwise, <c>false</c>.</value>
        [Browsable(false)]
        public bool OptionsUpdated
        {
            get
            {
                return _optionsUpdated;
            }
            set
            {
                _optionsUpdated = value;

                if (_optionsUpdated)
                {
                    OnOptionsChanged();
                }
            }
        }

        #endregion

        public OptionsPanel()
        {
            InitializeComponent();
        }

        virtual public void PanelAdded(OptionsForm optf)
        {
            OptionsForm = optf;

            InitPanelForControl(this);

            OptionsForm.OptionsSaving += OptionsSaving;
            OptionsForm.OptionsSaved += OptionsSaved;
            OptionsForm.ResetForm += ResetForm;
        }

        protected void SetOption(String optionName, Object value)
        {
            OptionsForm.AppSettings[optionName] = value;
            OptionsUpdated = true;
        }

        protected void OnOptionsChanged()
        {
            if (OptionsChanged != null)
            {
                OptionsChanged(this, EventArgs.Empty);
            }
        }

        protected void OptionsSaving(object sender, EventArgs e)
        {
        }

        protected void OptionsSaved(object sender, EventArgs e)
        {
            ReloadValues(this);
        }

        protected virtual void ResetForm(object sender, EventArgs e)
        {
            ReloadValues(this);
        }
        
        private static void ReloadValues(Control ctrl)
        {
            for (var i = 0; i < ctrl.Controls.Count; i++)
            {
                var ctrl2 = ctrl.Controls[i];

                for (var l = 0; l < ctrl2.DataBindings.Count; l++)
                {
                    var bind = ctrl2.DataBindings[l];
                    bind.ReadValue();
                }

                ReloadValues(ctrl2);
            }
        }

        private void InitPanelForControl(Control ctrl)
        {
            for (var i = 0; i < ctrl.Controls.Count; i++)
            {
                var ctrl2 = ctrl.Controls[i];

                for (var l = 0; l < ctrl2.DataBindings.Count; l++)
                {
                    var bind = ctrl2.DataBindings[l];
                    var prop = bind.BindingMemberInfo.BindingMember;

                    try
                    {
                        var value = OptionsForm.AppSettings[prop];

                        if (value != null)
                        {
                            bind.DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
                            bind.ControlUpdateMode = ControlUpdateMode.Never;
                            var sett = new System.Configuration.SettingsProperty(prop) {DefaultValue = value};
                            OptionsForm.Settings.Add(sett);
                        }
                    }
                    catch
                    { }
                }

                InitPanelForControl(ctrl2);
            }
        }
    }
}